package bb.lanxing.mvp.presetner.i;

import androidx.annotation.NonNull;

import bb.lanxing.manager.ISubscriptionManager;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

public abstract class BasePresenter implements IPresenter {
    private final ISubscriptionManager iSubscriptionManager = new ISubscriptionManager() {
        private final CompositeSubscription compositeSubscription = new CompositeSubscription();

        @Override
        public void unsubscribe() {
            compositeSubscription.unsubscribe();
        }

        @Override
        public void cancelSubscription(@NonNull Subscription subscription) {
            if (!subscription.isUnsubscribed()) {
                subscription.unsubscribe();
            }
            removeSubscription(subscription);
        }

        @Override
        public void removeSubscription(@NonNull Subscription subscription) {
            compositeSubscription.remove(subscription);
        }

        @Override
        public void addSubscription(@NonNull Subscription subscription) {
            compositeSubscription.add(subscription);
        }
    };

    public void addSubscription(Subscription subscription) {
        iSubscriptionManager.addSubscription(subscription);
    }

    public void cancelSubscription(Subscription subscription) {
        iSubscriptionManager.cancelSubscription(subscription);
    }

    @Override
    public void destroy() {
        iSubscriptionManager.unsubscribe();
    }
}